//
//  CTTabBarView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/5.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTTabBarView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "CTMessageLogic.h"

#define COLOR_NORMAL [UIColor colorWithRed:251/255.0 green:251/255.0 blue:249/255.0 alpha:1.0]
#define COLOR_CLICK [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0]

@interface CTTabBarView ()<CTMessageLogicDelegate>
{
    UIImageView * cameraBtn;
  
    UIView * homePageView;
    UIView * discoveryView;
    UIView * messageView;
    UIView * userView;
    
    UIImageView * homePageImageView;
    UIImageView * discoveryImageView;
    UIImageView * messageImageView;
    UIImageView * userImageView;
    
    
    UITapGestureRecognizer * cameraRecognizer;
    UITapGestureRecognizer * homepageRecognizer;
    UITapGestureRecognizer * discoveryRecognizer;
    UITapGestureRecognizer * messageRecognizer;
    UITapGestureRecognizer * userRecognizer;
    
    CTMessageLogic * mesgLogic;
    UIImageView * mesgMarkImageView;
    UILabel * mesgRemaindLabel;
}

@end

@implementation CTTabBarView

@synthesize delegate;

- (void)dealloc
{
    delegate = nil;
    
    mesgLogic = nil;
    mesgLogic.delegate = nil;
    
    [mesgMarkImageView removeFromSuperview];
    mesgMarkImageView = nil;
    [mesgRemaindLabel removeFromSuperview];
    mesgRemaindLabel = nil;

    [homepageRecognizer removeTarget:self action:@selector(selectBtnAction:)];
    homepageRecognizer = nil;
    [discoveryRecognizer removeTarget:self action:@selector(selectBtnAction:)];
    discoveryRecognizer = nil;
    [messageRecognizer removeTarget:self action:@selector(selectBtnAction:)];
    messageRecognizer = nil;
    [userRecognizer removeTarget:self action:@selector(selectBtnAction:)];
    userRecognizer = nil;
    
    
    [cameraBtn removeFromSuperview];
    cameraBtn = nil;
    
    [homePageView removeFromSuperview];
    homePageView = nil;
    [discoveryView removeFromSuperview];
    discoveryView = nil;
    [messageView removeFromSuperview];
    messageView = nil;
    [userView removeFromSuperview];
    userView = nil;
    
    [homePageImageView removeFromSuperview];
    homePageImageView = nil;
    [discoveryImageView removeFromSuperview];
    discoveryImageView = nil;
    [messageImageView removeFromSuperview];
    messageImageView = nil;
    [userImageView removeFromSuperview];
    userImageView = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CTPushNotification object:nil];
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTTabBarView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, CT_HEADER_SCREEN_HEIGHT-50, CT_HEADER_SCREEN_WIDTH, 50);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        self.alpha = 0.98;
        
        mesgLogic = [[CTMessageLogic alloc] init];
        mesgLogic.delegate = nil;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotiMesg:) name:CTPushNotification object:nil];
        
        [self creatSubViews];
    }
    return self;
}

-(void)creatSubViews
{
    UIView * borderLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 0.5)];
    borderLineView.backgroundColor = CT_HEADER_GRAY_COLOR;
    [self addSubview:borderLineView];
    
    
    
    cameraBtn = [[UIImageView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH - 75)/2.0,0.5,75,49.5)];
    cameraBtn.image = [UIImage imageNamed:@"tab_camera"];
    cameraBtn.userInteractionEnabled = YES;
    cameraBtn.tag = 100;
    [self addSubview:cameraBtn];

    
    
    homePageView = [[UIView alloc] initWithFrame:CGRectMake(0,0.5, (CT_HEADER_SCREEN_WIDTH - 75)/4.0, 50-0.5)];
    homePageView.backgroundColor = COLOR_CLICK;
    homePageView.tag = 101;
    [self addSubview:homePageView];
    homePageImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_home_click@3x"]];
    homePageImageView.frame = CGRectMake((homePageView.bounds.size.width - 27)/2.0,(homePageView.bounds.size.height - 27)/2.0, 27.0, 27.0);
    [homePageView addSubview: homePageImageView];
    
    discoveryView = [[UIView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH - 75)/4.0,0.5, (CT_HEADER_SCREEN_WIDTH - 75)/4.0, 50-0.5)];
    discoveryView.backgroundColor = COLOR_NORMAL;
    discoveryView.tag = 102;
    [self addSubview:discoveryView];
    discoveryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_faxian@3x"]];
    discoveryImageView.frame = CGRectMake((discoveryView.bounds.size.width - 27)/2.0,(discoveryView.bounds.size.height - 27)/2.0, 27, 27);
    [discoveryView addSubview: discoveryImageView];
    
    messageView = [[UIView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH + 75)/2.0 ,0.5, (CT_HEADER_SCREEN_WIDTH - 75)/4.0, 50-0.5)];
    messageView.backgroundColor = COLOR_NORMAL;
    messageView.tag = 103;
    [self addSubview:messageView];
    messageImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_inform@3x"]];
    messageImageView.frame = CGRectMake((messageView.bounds.size.width - 27)/2.0,(messageView.bounds.size.height - 27)/2.0, 27, 27);
    [messageView addSubview: messageImageView];
    
    userView = [[UIView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH + 75)/2.0 + (CT_HEADER_SCREEN_WIDTH - 75)/4.0,0.5, (CT_HEADER_SCREEN_WIDTH - 75)/4.0, 50-0.5)];
    userView.backgroundColor = COLOR_NORMAL;
    userView.tag = 104;
    [self addSubview:userView];
    userImageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_profile@3x"]];
    userImageView.frame = CGRectMake((userView.bounds.size.width - 27)/2.0,(userView.bounds.size.height - 27)/2.0, 27.0, 27.0);
    [userView addSubview: userImageView];
    
    
    
    
    cameraRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectBtnAction:)];
    cameraRecognizer.numberOfTapsRequired = 1;
    [cameraBtn addGestureRecognizer:cameraRecognizer];
    
    homepageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectBtnAction:)];
    homepageRecognizer.numberOfTapsRequired = 1;
    [homePageView addGestureRecognizer:homepageRecognizer];
    
    discoveryRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectBtnAction:)];
    discoveryRecognizer.numberOfTapsRequired = 1;
    [discoveryView addGestureRecognizer:discoveryRecognizer];
    
    messageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectBtnAction:)];
    messageRecognizer.numberOfTapsRequired = 1;
    [messageView addGestureRecognizer:messageRecognizer];
    
    userRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectBtnAction:)];
    userRecognizer.numberOfTapsRequired = 1;
    [userView addGestureRecognizer:userRecognizer];
    
    
    
    //消息
    mesgMarkImageView = [[UIImageView alloc] initWithFrame:CGRectMake((messageView.bounds.size.width - 27)/2.0+27-10,(messageView.bounds.size.height - 24)/2.0-5, 23, 15)];
    mesgMarkImageView.image= [UIImage imageNamed:@"weiduxiaoxi_bg"];
    [messageView addSubview:mesgMarkImageView];
    mesgMarkImageView.hidden = YES;
    mesgRemaindLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,2.5, 23, 10)];
    mesgRemaindLabel.font = [UIFont systemFontOfSize:10.0];
    mesgRemaindLabel.textAlignment = NSTextAlignmentCenter;
    mesgRemaindLabel.textColor = CT_HEADER_WHITE_COLOR;
    [mesgMarkImageView addSubview:mesgRemaindLabel];
}



#pragma mark - action & delegate -

-(void)selectIndex:(NSInteger)index
{
    switch (index) {
      
        case 0:
            [self reloadView];
            homePageImageView.image = [UIImage imageNamed:@"tab_home_click@3x"];
            homePageView.backgroundColor = COLOR_CLICK;
            break;
        case 1:
            [self reloadView];
            discoveryImageView.image = [UIImage imageNamed:@"tab_faxian_click@3x"];
            discoveryView.backgroundColor = COLOR_CLICK;
            break;
        case 2:
            [self reloadView];
            messageImageView.image = [UIImage imageNamed:@"tab_inform_click@3x"];
            messageView.backgroundColor = COLOR_CLICK;
            break;
        case 3:
            [self reloadView];
            userImageView.image = [UIImage imageNamed:@"tab_profile_click@3x"];
            userView.backgroundColor = COLOR_CLICK;
            break;
            
        default:
            break;
    }
}

-(void)selectBtnAction:(id)sender
{
    UITapGestureRecognizer * recognizer = (UITapGestureRecognizer *)sender;
    UIView * superView = recognizer.view;
    
    CTTabBarViewType type = CTTabBarViewTypeCamera;
    
    switch (superView.tag) {
        case 100:
            type = CTTabBarViewTypeCamera;
            break;
        case 101:
            type = CTTabBarViewTypeHome;
            
            [self reloadView];
            homePageImageView.image = [UIImage imageNamed:@"tab_home_click@3x"];
            homePageView.backgroundColor = COLOR_CLICK;
            break;
        case 102:
            type = CTTabBarViewTypeDiscovery;
            
            [self reloadView];
            discoveryImageView.image = [UIImage imageNamed:@"tab_faxian_click@3x"];
            discoveryView.backgroundColor = COLOR_CLICK;
            break;
        case 103:
            type = CTTabBarViewTypeMessage;
            
            [self reloadView];
            messageImageView.image = [UIImage imageNamed:@"tab_inform_click@3x"];
            messageView.backgroundColor = COLOR_CLICK;
            break;
        case 104:
            type = CTTabBarViewTypeUserProfile;
            
            [self reloadView];
            userImageView.image = [UIImage imageNamed:@"tab_profile_click@3x"];
            userView.backgroundColor = COLOR_CLICK;
            break;
            
        default:
            break;
    }
    
    if ([self.delegate respondsToSelector:@selector(selectAction:withType:)]) {
        [self.delegate selectAction:self withType:type];
    }
}

-(void)reloadView
{
    homePageImageView.image = [UIImage imageNamed:@"tab_home@3x"];
    discoveryImageView.image = [UIImage imageNamed:@"tab_faxian@3x"];
    messageImageView.image = [UIImage imageNamed:@"tab_inform@3x"];
    userImageView.image = [UIImage imageNamed:@"tab_profile@3x"];
    
    homePageView.backgroundColor = COLOR_NORMAL;
    discoveryView.backgroundColor = COLOR_NORMAL;
    messageView.backgroundColor = COLOR_NORMAL;
    userView.backgroundColor = COLOR_NORMAL;
}

-(void)notiCount
{
    NSInteger mesgCount = [mesgLogic mesgTemporaryDataCount:CTMessageLogicTemporaryTypeALL];
    if (mesgCount != 0) {
        mesgMarkImageView.hidden = NO;
        mesgRemaindLabel.text = [NSString stringWithFormat:@"%ld",(long)mesgCount];
    }else{
        mesgMarkImageView.hidden = YES;
        mesgRemaindLabel.text = @"0";
    }
}


#pragma mark - mesg delegate -

-(void)pushNotiMesg:(id)sender
{
    NSInteger mesgCount = [mesgLogic mesgTemporaryDataCount:CTMessageLogicTemporaryTypeALL];
    if (mesgCount != 0) {
        mesgMarkImageView.hidden = NO;
        mesgRemaindLabel.text = [NSString stringWithFormat:@"%ld",(long)mesgCount];
    }else{
        mesgMarkImageView.hidden = YES;
        mesgRemaindLabel.text = @"0";
    }
}

-(void)mesgTemporaryAdded
{
    NSInteger mesgCount = [mesgLogic mesgTemporaryDataCount:CTMessageLogicTemporaryTypeALL];
    if (mesgCount != 0) {
        mesgMarkImageView.hidden = NO;
        mesgRemaindLabel.text = [NSString stringWithFormat:@"%ld",(long)mesgCount];
    }else{
        mesgMarkImageView.hidden = YES;
        mesgRemaindLabel.text = @"0";
    }
}
-(void)mesgTemporaryRemoved
{
    NSInteger mesgCount = [mesgLogic mesgTemporaryDataCount:CTMessageLogicTemporaryTypeALL];
    if (mesgCount != 0) {
        mesgMarkImageView.hidden = NO;
        mesgRemaindLabel.text = [NSString stringWithFormat:@"%ld",(long)mesgCount];
    }else{
        mesgMarkImageView.hidden = YES;
        mesgRemaindLabel.text = @"0";
    }
}

@end
